<?php
/**
 * @author		João Batista Neto
 * @package		rpo
 * @subpackage	ssh
 * @category	ssh, state
 */
namespace rpo\ssh;

use rpo\base\Object;
use rpo\base\String;
use rpo\ssh\interfaces\ISSHConnection;
use rpo\ssh\interfaces\ISSHAuthentication;
use rpo\ssh\interfaces\ISSHState;
use rpo\ssh\state\SSHStateClosed;

/**
 * Implementa uma conexão com um servidor remoto via SSH
 * @author		João Batista Neto
 * @final
 * @package		rpo
 * @subpackage	ssh
 * @category	ssh, state
 */
class SSHConnection extends Object implements ISSHConnection {
	/**
	 * @var ISSHState
	 */
	private $state;

	/**
	 * Cria o objeto de conexão
	 */
	public function __construct(){
		$this->state = new SSHStateClosed();
	}

	/**
	 * Faz a autenticação no servidor
	 * @param ISSHAuthentication $auth
	 * @return boolean
	 */
	public function authenticate( ISSHAuthentication $auth ){
		return $this->state->authenticate( $auth , $this );
	}

	/**
	 * Executa um comando no servidor
	 * @param String $command
	 * @return boolean
	 */
	public function execute( String $command ){
		return $this->state->execute( $command , $this );
	}

	/**
	 * Recupera a fingerprint do servidor
	 * @return String
	 */
	public function getFingerprint(){
		return new String( $this->state->getFingerprint( $this ) );
	}

	/**
	 * Abre uma conexão com um servidor remoto
	 * @param String $host
	 * @param integer $port
	 * @return boolean
	 */
	public function open( String $host , $port = 22 ){
		return $this->state->open( $host , $port , $this );
	}

	/**
	 * Modifica o estado da conexão
	 * @param ISSHState $state
	 */
	public function changeState( ISSHState $state ){
		$this->state =& $state;
	}
}